<template>
  <el-card class="cate-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb  separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加按钮 -->
    <el-row type="flex" justify="end">
      <el-col :span="2"><el-button type="primary" size="small" @click="open('add')">添加分类</el-button></el-col>
    </el-row>
    <!-- 添加分类信息的对话框 -->
    <el-dialog :title="title" :visible.sync="addDialogVisible" width="35%">
        <!-- 添加的表单 -->
        <el-form :model="form" :rules="rules"  label-width="80px" ref="form">
            <el-form-item label="分类名称" prop="cate_name">
                <el-input v-model="form.cate_name"></el-input>
            </el-form-item>
            <el-form-item label="分类别名" prop="cate_alias">
                <el-input v-model="form.cate_alias"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCate">添 加</el-button>
        </span>
    </el-dialog>
    <!-- 表格 -->
    <el-table :data="list">
      <el-table-column label="序号" type="index" width="120"></el-table-column>
      <el-table-column label="分类名称" prop="cate_name" ></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias" ></el-table-column>
      <el-table-column lable="操作">
        <template #default="{row}">
          <el-button type="primary" size="mini" @click="open('edit',row.id)">修改</el-button>
          <el-button type="danger" size="mini" @click="delList(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
import { reqGetArtCateList, addArticleList, delArticleList, getArticleDetail, updateArticle } from '@/api/article'
export default {
  name: 'ArtCategory',
  data () {
    return {
      // 开关
      addDialogVisible: false,
      type: 'add',
      list: [],
      form: {
        cate_name: '',
        cate_alias: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 10, message: '分类名称必须是2-10位的字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { min: 2, max: 10, message: '分类别名必须是2-10位的字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const res = await reqGetArtCateList()
      // console.log(res)
      this.list = res.data
    },
    async addCate () {
      // const res = await addArticleList(this.form)
      // console.log(res)
      // this.addDialogVisible = false
      // this.getList()
      this.$refs.form.validate(async (isOk) => {
        if (!isOk) return
        if (this.type === 'add') {
          await addArticleList(this.form)
          this.form = {}
        } else {
          await updateArticle(this.form)
          this.form = {}
        }
        // console.log(res)
        this.addDialogVisible = false
        this.getList()
      })
    },
    async delList (id) {
      await delArticleList(id)
      this.getList()
    },
    async open (type, id) {
      this.addDialogVisible = true
      this.type = type
      if (this.type === 'edit') {
        const res = await getArticleDetail(id)
        // console.log(res)
        this.form = res.data
      }
    }
  },
  computed: {
    title () {
      if (this.type === 'add') return '添加文章分类'
      return '修改文章分类'
    }
  }
}
</script>

<style>

</style>
